<?php
namespace app\madmin\controller;
use app\madmin\model\WechatReplyModel;
use app\madmin\model\WechatConfsModel;
use think\Controller;
use org\Wechat;

/**
 * 微信对接控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190201
 */
class WechatLinks extends Controller{

    # 访问空的方法
    public function _empty(){
        return $this->fetch('PublicBase/show404');
    }

    # 微信对接方法
	public function showLink(){
        $Config = $this->ConfigCache();

        if(!empty($Config['WECHAT_TOKEN'])){
            $Wechat = new Wechat($Config['WECHAT_TOKEN']);
            
            $Data = $Wechat->DataGain();

            list($Content, $Type) = $this->showReply($Data);
            
            $Wechat->DataSend($Content, $Type);
        }
    }

    # 微信信息方法
    private function showReply($Data){
        # 消息自动回复
        if(!empty($Data['Content'])){
            return $this->MessageCache($Data['Content']);
        }

        # 获取地理位置
        if($Data['Event'] == 'LOCATION'){
            return [lang('WxGnDd'), 'text'];
        }

        # 关注自动回复
        if($Data['Event'] == 'subscribe'){
            $Config = $this->ConfigCache();

            if(!empty($Config['WECHAT_FOLLOW'])){
                return $this->MessageCache($Config['WECHAT_FOLLOW']);
            }else{
                return [lang('WxWbCg'), 'text'];
            }
        }
        
        # 点击微信菜单
        if($Data['Event'] == 'CLICK'){
            return $this->MessageCache($Data['EventKey']);
        }
    }

    # 获取微信消息缓存信息
    private function MessageCache($Text){
        $Message = cache('WechatMessage');

        $Array = [];

        if(empty($Message[md5($Text)])){
            $Array = $this->MessageLists($Text);
        }else{
            $Array = $Message[md5($Text)];
        }

        switch($Array['key_type']){
            case '1':
                return [$Array['key_text'], 'text'];
                break;
            case '2':
                return [[[$Array['key_title'], $Array['key_info'], $Array['key_img'], $Array['key_url']]], 'news'];
                break;
            default:
                break;
        }
    }

    # 获取微信消息列表信息
    private function MessageLists($Text){
        $WechatReply = new WechatReplyModel;

        $FindKey = $WechatReply->mInfo(['key_name'=>$Text, 'key_state'=>1]);

        $Array = cache('WechatMessage');

        if(empty($FindKey)){
            $FindKey['key_type'] = '1';
            $FindKey['key_text'] = lang('WxWbCw');
        }else{
            $Array[md5($Text)] = $FindKey;

            cache('WechatMessage', $Array);
        }

        return $FindKey;
    }

    # 获取微信配置缓存信息
    private function ConfigCache(){
        $Config = cache('WechatConfig');

        if(empty($Config)){
            $Array = $this->ConfigLists();
        }else{
            $Array = $Config;
        }

        return $Array;
    }

    # 获取微信配置列表信息
    private function ConfigLists(){
        $WechatConfs = new WechatConfsModel;

        $Lists = $WechatConfs->mAll();

        $Array = [];

        if(!empty($Lists)){
            foreach($Lists as $k => $v){
                $Array[$v['config_name']] = $v['config_value'];
            }

            cache('WechatConfig', $Array);
        }

        return $Array;
    }

}